﻿namespace Nomo.UnityEditorModule
{
    [UnityEditor.CanEditMultipleObjects]
    [UnityEditor.CustomEditor(typeof(UnityCoreModule.UI.RoundedRectangle))]
    internal sealed class RoundedRectangleEditor : UnityEditor.UI.GraphicEditor
    {
        private UnityCoreModule.UI.RoundedRectangle _roundedRectangle;
        private UnityEditor.SerializedProperty      _texture;
        private UnityEditor.SerializedProperty      _segments;
        private UnityEditor.SerializedProperty      _normalized;
        private UnityEditor.SerializedProperty      _upperLeftCornerRadius;
        private UnityEditor.SerializedProperty      _upperRightCornerRadius;
        private UnityEditor.SerializedProperty      _lowerLeftCornerRadius;
        private UnityEditor.SerializedProperty      _lowerRightCornerRadius;
        private UnityEngine.GUIContent              _cornerRadii;
        private UnityEngine.GUIContent              _normalizedGUIContent;
        private UnityEngine.GUIContent              _upperLeftCornerRadiusGUIContent;
        private UnityEngine.GUIContent              _upperRightCornerRadiusGUIContent;
        private UnityEngine.GUIContent              _lowerLeftCornerRadiusGUIContent;
        private UnityEngine.GUIContent              _lowerRightCornerRadiusGUIContent;
        private bool                                _foldOut = true;

        protected override void OnEnable()
        {
            base.OnEnable();
            _roundedRectangle                 = serializedObject.targetObject as UnityCoreModule.UI.RoundedRectangle;
            _texture                          = serializedObject.FindProperty("texture");
            _segments                         = serializedObject.FindProperty("segments");
            _normalized                       = serializedObject.FindProperty("normalized");
            _upperLeftCornerRadius            = serializedObject.FindProperty("upperLeftCornerRadius");
            _upperRightCornerRadius           = serializedObject.FindProperty("upperRightCornerRadius");
            _lowerLeftCornerRadius            = serializedObject.FindProperty("lowerLeftCornerRadius");
            _lowerRightCornerRadius           = serializedObject.FindProperty("lowerRightCornerRadius");
            _cornerRadii                      = new UnityEngine.GUIContent(nameof(UnityCoreModule.UI.RoundedRectangle.CornerRadii), "圆角半径");
            _normalizedGUIContent             = new UnityEngine.GUIContent(nameof(UnityCoreModule.UI.RoundedRectangle.Normalized));
            _upperLeftCornerRadiusGUIContent  = new UnityEngine.GUIContent(nameof(UnityCoreModule.UI.RoundedRectangle.UpperLeftCornerRadius),  "左上方圆角半径");
            _upperRightCornerRadiusGUIContent = new UnityEngine.GUIContent(nameof(UnityCoreModule.UI.RoundedRectangle.UpperRightCornerRadius), "右上方圆角半径");
            _lowerLeftCornerRadiusGUIContent  = new UnityEngine.GUIContent(nameof(UnityCoreModule.UI.RoundedRectangle.LowerLeftCornerRadius),  "左下方圆角半径");
            _lowerRightCornerRadiusGUIContent = new UnityEngine.GUIContent(nameof(UnityCoreModule.UI.RoundedRectangle.LowerRightCornerRadius), "右下方圆角半径");
            SetShowNativeSize(_texture.objectReferenceValue != null, true);
        }

        public override void OnInspectorGUI()
        {
            serializedObject.Update();
            UnityEditor.EditorGUILayout.PropertyField(_texture);
            AppearanceControlsGUI();
            UnityEditor.EditorGUILayout.PropertyField(_segments);
            UnityEditor.EditorGUILayout.Space();
            if (_foldOut = UnityEditor.EditorGUILayout.BeginFoldoutHeaderGroup(_foldOut, _cornerRadii, UnityEditor.EditorStyles.foldout))
            {
                ++UnityEditor.EditorGUI.indentLevel;
                UnityEditor.EditorGUI.BeginChangeCheck();
                UnityEditor.EditorGUILayout.PropertyField(_normalized, _normalizedGUIContent);
                if (UnityEditor.EditorGUI.EndChangeCheck())
                {
                    var upperLeftCornerRadius  = _upperLeftCornerRadius.floatValue;
                    var upperRightCornerRadius = _upperRightCornerRadius.floatValue;
                    var lowerLeftCornerRadius  = _lowerLeftCornerRadius.floatValue;
                    var lowerRightCornerRadius = _lowerRightCornerRadius.floatValue;
                    var r                      = _roundedRectangle.GetPixelAdjustedRect();
                    var minSide                = UnityEngine.Mathf.Min(r.width, r.height);
                    switch (_normalized.boolValue)
                    {
                        case true:
                            _upperLeftCornerRadius.floatValue  = minSide != 0F ? UnityEngine.Mathf.Clamp(upperLeftCornerRadius  / minSide, 0F, 0.5F) : 0F;
                            _upperRightCornerRadius.floatValue = minSide != 0F ? UnityEngine.Mathf.Clamp(upperRightCornerRadius / minSide, 0F, 0.5F) : 0F;
                            _lowerLeftCornerRadius.floatValue  = minSide != 0F ? UnityEngine.Mathf.Clamp(lowerLeftCornerRadius  / minSide, 0F, 0.5F) : 0F;
                            _lowerRightCornerRadius.floatValue = minSide != 0F ? UnityEngine.Mathf.Clamp(lowerRightCornerRadius / minSide, 0F, 0.5F) : 0F;
                            break;
                        default:
                            _upperLeftCornerRadius.floatValue  = upperLeftCornerRadius  * minSide;
                            _upperRightCornerRadius.floatValue = upperRightCornerRadius * minSide;
                            _lowerLeftCornerRadius.floatValue  = lowerLeftCornerRadius  * minSide;
                            _lowerRightCornerRadius.floatValue = lowerRightCornerRadius * minSide;
                            break;
                    }
                }
                switch (_normalized.boolValue)
                {
                    case true:
                        UnityEditor.EditorGUILayout.Slider(_upperLeftCornerRadius,  0F, 0.5F, _upperLeftCornerRadiusGUIContent);
                        UnityEditor.EditorGUILayout.Slider(_upperRightCornerRadius, 0F, 0.5F, _upperRightCornerRadiusGUIContent);
                        UnityEditor.EditorGUILayout.Slider(_lowerLeftCornerRadius,  0F, 0.5F, _lowerLeftCornerRadiusGUIContent);
                        UnityEditor.EditorGUILayout.Slider(_lowerRightCornerRadius, 0F, 0.5F, _lowerRightCornerRadiusGUIContent);
                        break;
                    default:
                        var r       = _roundedRectangle.GetPixelAdjustedRect();
                        var minSide = UnityEngine.Mathf.Min(r.width, r.height);
                        UnityEditor.EditorGUILayout.Slider(_upperLeftCornerRadius,  0F, minSide * 0.5F, _upperLeftCornerRadiusGUIContent);
                        UnityEditor.EditorGUILayout.Slider(_upperRightCornerRadius, 0F, minSide * 0.5F, _upperRightCornerRadiusGUIContent);
                        UnityEditor.EditorGUILayout.Slider(_lowerLeftCornerRadius,  0F, minSide * 0.5F, _lowerLeftCornerRadiusGUIContent);
                        UnityEditor.EditorGUILayout.Slider(_lowerRightCornerRadius, 0F, minSide * 0.5F, _lowerRightCornerRadiusGUIContent);
                        break;
                }
                --UnityEditor.EditorGUI.indentLevel;
                UnityEditor.EditorGUILayout.EndFoldoutHeaderGroup();
            }
            UnityEditor.EditorGUILayout.Space();
            RaycastControlsGUI();
            SetShowNativeSize(_texture.objectReferenceValue != null, false);
            NativeSizeButtonGUI();
            serializedObject.ApplyModifiedProperties();
        }
    }
}
